package com.zcyh.exam.controller.admin.exam;


import cn.hutool.core.util.StrUtil;
import com.zcyh.exam.annotation.param.Param;
import com.zcyh.exam.annotation.param.ParamsType;
import com.zcyh.exam.annotation.param.ParamsValidate;
import com.zcyh.exam.constants.CacheKey;
import com.zcyh.exam.controller.BaseController;
import com.zcyh.exam.model.common.Result;
import com.zcyh.exam.model.common.ResultCode;
import com.zcyh.exam.model.dto.TestPaperQuestionDto;
import com.zcyh.exam.model.entity.TestPaperInfo;
import com.zcyh.exam.model.entity.TestPaperQuestionInfo;
import com.zcyh.exam.model.request.PageParam;
import com.zcyh.exam.model.request.TestPaperQuestionRequest;
import com.zcyh.exam.service.education.TestPaperInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 试卷管理
 * @author zengjintao
 * @version 1.0
 * @create_at 2020/11/20 21:21
 */
@RestController
@RequestMapping("/system/testPaperInfo")
public class TestPaperInfoController extends BaseController {

    @Autowired
    private TestPaperInfoService testPaperInfoService;

    /**
     * 试卷列表
     * @param pageParam
     * @param areaCodes code逗号分隔
     * @param status status 0未开始 1进行中 2一结束
     * @param startTime
     * @param endTime
     * @param name
     * @return
     */
    @GetMapping
    public Result list(PageParam pageParam,
                       @RequestParam(required = false) String areaCodes,
                       @RequestParam(required = false) Integer status,
                       @RequestParam(required = false) String startTime,
                       @RequestParam(required = false) String endTime,
                       @RequestParam(required = false) String name,
                       @RequestParam(required = false) boolean publishFlag
    ) {
        return Result.success(testPaperInfoService.selectPageList(pageParam, areaCodes,name,startTime,endTime,status,publishFlag));
    }

    /**
     * 获取试卷详情
     * @param testPaperInfoId
     * @return
     */
    @GetMapping("getTestPaperInfo/{testPaperInfoId}")
    public Result gettestPaperInfo(@PathVariable String testPaperInfoId) {
        return Result.success(testPaperInfoService.getTestPaperInfo(testPaperInfoId));
    }

    /**
     * 添加或修改试卷
     * 修改由前端传入参考人数
     * @param testPaperInfo
     * @return
     */
    @PostMapping
    @ParamsValidate(params = {
            @Param(name = "examNumber", message = "参考人数不能为空")
    }, paramsType = ParamsType.JSON_DATA)
    public Result saveOrUpdate(@RequestBody @Validated TestPaperInfo testPaperInfo) {
        if (testPaperInfo.getId() != null) {
            TestPaperInfo dataBaseTestPaperInfo = testPaperInfoService.getById(testPaperInfo.getId());
            if(dataBaseTestPaperInfo==null){
                return Result.fail(ResultCode.FAIL, "试卷不存在");
            }
            if (dataBaseTestPaperInfo.getExamNumber() > 0) {
                return Result.fail(ResultCode.FAIL, "试卷已被使用, 无法修改");
            }
        }
        if(testPaperInfo.getType()==0 && StrUtil.isBlank(testPaperInfo.getAreaCodes())){
            return Result.fail(ResultCode.FAIL, "区域不能为空");
        }else if(testPaperInfo.getType()==1 && StrUtil.isBlank(testPaperInfo.getUsers())){
            return Result.fail(ResultCode.FAIL, "网格员不能为空");
        }else if(testPaperInfo.getType()>1){
            return Result.fail(ResultCode.FAIL, "type类型错误");
        }
        if(testPaperInfo.getType()==1){
            testPaperInfo.setAreaCodes("");
        }else{
            testPaperInfo.setUsers("");
        }
        testPaperInfoService.saveOrUpdate(testPaperInfo);
        return Result.success();
    }
    /**
     * 保存试卷试题
     * @param testPaperQuestionInfoList
     * @return
     */
    @PostMapping("saveTestPaperInfoQuestion")
    @CacheEvict(
            cacheNames = CacheKey.TEST_PAPER_INFO_CACHE,
            key = "#testPaperQuestionInfoList.get(0).testPaperInfoId"
    )
    public Result saveTestPaperInfoQuestion(@RequestBody List<TestPaperQuestionInfo> testPaperQuestionInfoList) {
        testPaperInfoService.saveTestPaperInfoQuestion(testPaperQuestionInfoList);
        return Result.success();
    }

    /**
     * 获取试卷试题列表
     * @param testPaperQuestionRequest
     * @return
     */
    @GetMapping("getPaperQuestionList")
    @ParamsValidate(params = {
            @Param(name = "testPaperInfoId", message = "试卷id不能为空")
    }, paramsType = ParamsType.FORM_DATA)
    @Cacheable(cacheNames = CacheKey.TEST_PAPER_INFO_CACHE, key = "#testPaperQuestionRequest.testPaperInfoId")
    public Result selectPaperQuestionList(PageParam pageParam, TestPaperQuestionRequest testPaperQuestionRequest) {
        return Result.success(testPaperInfoService.selectPaperQuestionList(pageParam, testPaperQuestionRequest));
    }

    /**
     * 修改试卷试题分数或者排序
     * @param testPaperQuestionDto
     * @return
     */
    @PostMapping("updatePaperQuestionMarkOrSort")
    @ParamsValidate(params = {
            @Param(name = "testPaperInfoId", message = "试卷id不能为空"),
            @Param(name = "questionInfoId", message = "问题id不能为空"),
            @Param(name = "mark", message = "mark不能为空"),
            @Param(name = "sort", message = "sort不能为空"),
            @Param(name = "updateType", message = "updateType不能为空")
    }, paramsType = ParamsType.JSON_DATA)
    @CacheEvict(cacheNames = CacheKey.TEST_PAPER_INFO_CACHE, key = "#testPaperQuestionDto.testPaperInfoId")
    public Result updatePaperQuestionMarkOrSort(@RequestBody TestPaperQuestionDto testPaperQuestionDto) {
        testPaperInfoService.updatePaperQuestionMarkOrSort(testPaperQuestionDto);
        return Result.success();
    }

    /**
     * 发布试卷
     * @param testPaperInfoId
     * @return
     */
    @PostMapping("publishTestPaperInfo/{testPaperInfoId}")
    public Result publishTestPaperInfo(@PathVariable String testPaperInfoId) {
        return Result.success(testPaperInfoService.publishTestPaperInfo(testPaperInfoId));
    }

    /**
     * 撤销试卷
     * @param testPaperInfoId
     * @return
     */
    @PostMapping("cancelTestPaperInfo/{testPaperInfoId}")
    public Result cancelTestPaperInfo(@PathVariable String testPaperInfoId) {
        return Result.success(testPaperInfoService.cancelTestPaperInfo(testPaperInfoId));
    }


    /**
     * 删除试卷
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    @CacheEvict(cacheNames = CacheKey.TEST_PAPER_INFO_CACHE, key = "#id")
    public Result deleteById(@PathVariable String id) {
        return Result.success(testPaperInfoService.deleteById(id));
    }

    /**
     * 移除试卷试题
     * @param testPaperQuestionInfo
     * @return
     */
    @DeleteMapping("removePaperQuestion")
    @ParamsValidate(params = {
            @Param(name = "testPaperInfoId", message = "试卷id不能为空"),
            @Param(name = "questionInfoId", message = "问题id不能为空"),
            @Param(name = "mark", message = "mark不能为空")
    }, paramsType = ParamsType.JSON_DATA)
    @CacheEvict(cacheNames = CacheKey.TEST_PAPER_INFO_CACHE, key = "#testPaperQuestionInfo.testPaperInfoId")
    public Result removePaperQuestion(@RequestBody TestPaperQuestionInfo testPaperQuestionInfo) {
        return Result.success(testPaperInfoService.removePaperQuestion(testPaperQuestionInfo));
    }

}
